//
//  ST_BTVideoParticularViewController+Barrage.swift
//  BTVideo
//
//  Created by DouLei on 2019/8/14.
//  Copyright © 2019 DouLei. All rights reserved.
//

import Foundation
import SnapKit
import SwifterSwift
import SnapKit
import AVFoundation
import MediaPlayer
import AudioToolbox
import HandyJSON
import SDWebImage
import SwiftyAttributes
import SwiftyJSON

//第一次加载弹幕的数量
private let InitBarrageNumbers = 2


var BarrageColorArray = [Color.init(hexString: "#ECA137")!,Color.init(hexString: "#E85C83")!,Color.init(hexString: "#5C73E8")!,Color.init(hexString: "#D05CE8")!]

extension ST_BTVideoParticularViewController:UITextFieldDelegate{
    
    
    @objc func ready_play_barrage_handle(noti:Notification){
     
        if  JSON(noti.userInfo ?? [:])["videoId"].stringValue == model.id{
            
            self.display_barrage()
        }
    }
    
    
    
    func display_barrage()  {
        
        

        
        self.barrageIdx = InitBarrageNumbers
        
        if let list = ST_BT2KBarrageAdapter.shared.find_top_barrages(videoId: model.id){
          
            self.top_barrage_container_view.data_array = list
          
            self.top_barrage_container_view.play()
        }
        
        if let list = ST_BT2KBarrageAdapter.shared.find_middle_barrages(videoId: model.id){
            
            self.mid_barrage_container_view.data_array = list
            
            self.mid_barrage_container_view.play()
        }
        
        if let list = ST_BT2KBarrageAdapter.shared.find_bottom_barrages(videoId: model.id){
            
            self.bottom_barrage_container_view.data_array = list
            
            self.bottom_barrage_container_view.play()
            
        }
        
    }
    
//    MARK:- textFiledDelegate
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        
        if (string == "\n") {
            
            currentBarrage.text = textField.text ?? ""
            
            textField.resignFirstResponder()
            
            textField.text = ""
            
            currentBarrage.color = BarrageColorArray.randomElement()!
            
            self.send_barrage()
            
            return false
        }
        
        let currentString: NSString = textField.text! as NSString
        
        let newString: NSString =
            currentString.replacingCharacters(in: range, with: string) as NSString
        
        return newString.length <= BARRAGE_MAX_LENGTH
    }
    
    func send_barrage()  {
        
        let barrage = ST_BarrageModel.convert_from(editBarrage: self.currentBarrage)
        
        if self.currentBarrage.direction == .bottom {
            
            //            print("发弹幕的颜色:",barrage.reply_color)
            
            self.bottom_barrage_container_view.insert(barrage: barrage)
            
        }else if self.currentBarrage.direction == .middle {
            
            //            print("发弹幕的颜色:",barrage.reply_color)
            
            self.mid_barrage_container_view.insert(barrage: barrage)
            
        }else{
            
            self.top_barrage_container_view.insert(barrage: barrage)
            
        }
        
        let param = ["video_id":self.model.id,
                     "content":self.currentBarrage.text,
                     "color":barrage.reply_color,
                     "position":self.currentBarrage.direction.rawValue] as [String : Any]
        
        JVrequest(PH.sendBarrageApi,parameters:param ,success: { (json) in
            
            
//            ToastHud(text: "发送弹幕成功", view: self.view)
            
        }) { (code, err) in
            
            print("发送弹幕failure:\(err)")
            
//            ToastHud(text: "发送弹幕失败", view: self.view)
        }
        
        
    }
    
}
